<?php

namespace App\PartyRequest;

use App\Http\Controllers\Controller;
use App\Models\Mysql\XnOrder;
use App\Models\Mysql\XnTerraceInfo;
use App\Models\Admin\InterfaceConfiguration;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Exception;
use function Symfony\Component\Translation\t;

class RequestOrder extends Controller
{
    //yipai
    public function yipai($data)
    {
        try{
            $supplier_info = $this->supplier_info($data['order']['goods_belong']);
            $array = [
                'businessId' => $supplier_info['key']['app_id'],//商户号
                'userOrderId' => $data['order']['order_sn'],//订单号
                'goodsId' => $data['info']['goods_code'],//商品编号id
                'userName' => $data['order']['account'],//要充值的用户
                'goodsNum' => $data['order']['goods_num'],//充值数量
                'orderIp' =>$data['info']['user_ip'],//获取用户当前的ip
                'unitPrice' => $data['order']['unitPrice'],//商品的单价
                'noticeUrl' => 'https://'.$data['info']['url'].'/notice/yipai',//异步回调地址
            ];
            $please['sign'] = md5(strtolower($array['businessId'] . $array['userOrderId'] . $array['goodsId'] . $array['goodsNum'] . $array['orderIp']) . $supplier_info['key']['key']);//生成的sign签名
            $response = $this->XML_array(Http::asForm()->post($supplier_info['url'],$array));
            $body =$response;
            $this->recharge_info($data['info']['url'],$supplier_info['url'],$array,$body);
            $this->msg($data['order']['order_sn'],$body['mes']);
        }catch (\Throwable $e){
            Log::channel('place_request')->error($e->getMessage());
        }
    }
    //kameng
    public function kameng($data)
    {
        try{
            $supplier_info = $this->supplier_info($data['order']['goods_belong']);
            $array = [
                'customer_id' => $supplier_info['key']['app_id'],//商户号
                'notify_url'=>'https://'.$data['info']['url'].'/notice/kameng',//异步回调地址
                'outer_order_id'=>$data['order']['order_sn'],//订单号
                'product_id' =>$data['info']['goods_code'],//商品编号id
                'quantity' => $data['order']['goods_num'],//充值数量
                'recharge_account' => $data['order']['account'],//要充值的用户
                'safe_cost'=>$data['order']['unitPrice'],//商品的单价
                'timestamp'=>time(),//时间戳
            ];
            $str = '';
            foreach ($array as $k => $v) {
                $str .= $k . $v;
            }
            $array['sign'] =  md5( $supplier_info['key']['key'] . $str);
            $response = Http::asForm()->post($supplier_info['url'],$array);
            $body = $response->json();
            $this->recharge_info($data['info']['url'],$supplier_info['url'],$array,$body);
            $this->msg($data['order']['order_sn'],$body['message']);
        }catch (\Throwable $e){
            Log::channel('place_request')->error($e->getMessage());
        }
    }




    #****#
    //写入充值信息
    private function msg($order,$msg)
    {
        $res = XnOrder::where('order_sn',$order)->update(['msg'=>$msg]);
        if (!$res) {
            throw new Exception($order."充值信息写入失败");
        }
    }
    //记录下单信息
    private function recharge_info($place_url,$url,$array,$body)
    {
        $data = [
            '供应商'=>$place_url,
            '请求地址'=>$url,
            '请求数据'=>$array,
            '返回数据'=>$body
        ];
        Log::channel('recharge_info')->error( json_encode($data,320));
    }
    //获取供应商配置
    private function supplier_info($goods_belong){
        $belong = InterfaceConfiguration::where('id',$goods_belong)->first();
        if (!$belong) {
            throw new Exception("未找到相关第三方配置信息");
        }
        $key = XnTerraceInfo::where(['sid'=>$goods_belong,'status'=>1])->first();
        if (empty($key['datas'])) {
            throw new Exception("未找到供应商配置密钥");
        }
        return  $data = [
            'key'=>json_decode($key['datas'],true),
            'url'=>$belong['recharge_url'],
        ];
    }
}
